package com.lab.utils;

import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;


import com.lab.logtrack.AppLog;
import com.lab.rtenvironment.AppCoreRuntime;

import java.lang.reflect.Field;

/**
 * create time：2013-2-14 下午7:02:53
 *
 * @version 1.0 description：
 * @since JDK 1.6
 */
public class DisplayUtil {
    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param pxValue
     * @param scale
     * （DisplayMetrics类中属性density）
     * @return
     */

    static public DisplayMetrics metrics;

    static {
        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) AppCoreRuntime.context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
        metrics = dm;
        AppLog.df("get DisplayMetrics %d, %d", metrics.widthPixels, metrics.heightPixels);
        if (metrics.widthPixels > metrics.heightPixels) {
            AppLog.d("rotate metrics!!!!");
            int oldWidth = metrics.widthPixels;
            metrics.widthPixels = metrics.heightPixels;
            metrics.heightPixels = oldWidth;
        }
    }

    private static int statusBarHeight = 0;
    private static int naviBarHeight = 0;

    private static int smartbarShownHeight = 0;

    public static void setSmartbarShownHeight(int height){
        smartbarShownHeight = height;
    }

    public static DisplayMetrics getMetrics() {
        return metrics;
    }

    public static int px2dip(float pxValue) {
        return (int) (pxValue / metrics.density + 0.5f);
    }

    /**
     * 使从全屏activity到非全屏activity时，不出现因为status bar而卡顿的情况
     *
     * @param window
     */
    public static void hackExitFullScreenTransition(Window window) {
        makeWindowNoLimit(window);
    }

    public static void toggleFullscreen(Window window, boolean fullscreen) {
        WindowManager.LayoutParams attrs = window.getAttributes();
        if (fullscreen) {
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
        window.setAttributes(attrs);
    }

    public static int getStatusBarHeight() {
        if (statusBarHeight == 0) {
            statusBarHeight = getBarHeight();
        }
        return statusBarHeight;
    }

    /**
     * KitKat 版本之上使用半透明状态栏，播放器计算动画时状态栏高度应为0
     * @return
     */
    public static int getTranslucentStatusBarShowHeight() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return 0;
        } else {
            return getStatusBarHeight();
        }
    }

    public static int getNavigationBarHeight() {
        if (naviBarHeight == 0) {
            naviBarHeight = getNavigationBar();
        }
        return naviBarHeight;
    }

    private static int getBarHeight() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 38;//默认为38，貌似大部分是这样的
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = AppCoreRuntime.context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sbar;
    }

    private static int getNavigationBar() {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, navBar = 0;//默认为38，貌似大部分是这样的
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("navigation_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            navBar = AppCoreRuntime.context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return navBar;
    }

    public static void makeWindowNoLimit(Window window) {
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        window.setAttributes(attributes);
    }


    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue （DisplayMetrics类中属性density）
     * @return
     */
    public static int dip2px(float dipValue) {
        if (metrics != null)
            return (int) (dipValue * metrics.density + 0.5f);
        return 0;
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(float pxValue) {
        return (int) (pxValue / metrics.scaledDensity + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(float spValue) {
        return (int) (spValue * metrics.scaledDensity + 0.5f);
    }

    public static int getDisplayHeight() {
        return metrics.heightPixels - smartbarShownHeight;
    }

    public static int getDisplayWidth() {
        return metrics.widthPixels;
    }

}
